﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.PublicAffair.Data;
using CentralOffice.PublicAffair.BusinessActions;
using PostTable = CentralOffice.PublicAffair.Data.PostDataSet.PostsDataTable;
using PostRow = CentralOffice.PublicAffair.Data.PostDataSet.PostsRow;

using PostReplyTable = CentralOffice.PublicAffair.Data.PostReplyDataSet.PostReplyDataTable;
using PostReplyRow = CentralOffice.PublicAffair.Data.PostReplyDataSet.PostReplyRow;
using System.Data;


namespace CentralOffice.WebClient.PublicAffairs
{
    public partial class PostView : PageBase
    {
        private bool? canManage = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                string postId = HttpContext.Current.Request.QueryString["id"];
                PostRow post = CentralOffice.PublicAffair.BusinessActions.Post.GetById(new Guid(postId));
                lblTitle.Text = post.Name;
                lblPostTitle.Text = post.Name;
            }

            if (canManage == null)
            {
                canManage = this.Authorize(AuthorizationRules.Post_Manage);
            }
        }

        protected void btnReplay_Click(object sender, EventArgs e)
        {
            string postId = HttpContext.Current.Request.QueryString["id"];

            PostReplyDataSet replyDataSet = new PostReplyDataSet();

            AddReplyRow(replyDataSet, true, new Guid(postId));

            CentralOffice.PublicAffair.BusinessActions.PostReply.Update(replyDataSet);

            txtMessage.Value = "";
            txtName.Text = "";

        }

        private void AddReplyRow(PostReplyDataSet replyDataSet, bool isNew, Guid postId)
        {
            PostReplyRow row = null;
            if (isNew)
            {
                row = replyDataSet.PostReply.NewPostReplyRow();
                row.Id = Guid.NewGuid();

                row.PostId = postId;
                row.ReplyBy = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
                row.ReplyDate = DateTime.Now;
                row.ReplyText = txtMessage.Value;
                row.ReplyTitle = txtName.Text;

                replyDataSet.PostReply.AddPostReplyRow(row);
            }
            else
            {
                row = CentralOffice.PublicAffair.BusinessActions.PostReply.GetById(new Guid(Request.QueryString["id"]));

                //CentralOffice.PublicAffair.BusinessActions.PostReply.GetByMasterId()

                
                row.PostId = postId;
                row.ReplyBy = CommonUtil.TrimAll(ApplicationContext.ActualUserName);
                row.ReplyDate = DateTime.Now;
                row.ReplyText = txtMessage.Value;
                row.ReplyTitle = txtName.Text;
            }

            string fileName = SaveFile(SiteUtil.GetPostPath(), FileUpload1);
            if (fileName.Length > 0)
            {
                row.Attachments = fileName;
            }
        }

        private string SaveFile(string rootDir, FileUpload file)
        {
            string fileName = "";

            string path = Server.MapPath(rootDir);
            if (file.HasFile)
            {
                //file.SaveAs(path + file.FileName);
                //fileName = rootDir + file.FileName;
                var fn = SiteUtil.GetUploadFileName(path, file.FileName);
                file.SaveAs(fn);
                fileName = rootDir + fn.Substring(fn.LastIndexOf('\\') + 1);
            }
            else
            {

            }
            return fileName;
        }

        [System.Web.Services.WebMethod]
        public static string DeleteComment(string id)
        {
            string id1 = HttpContext.Current.Request.Form["id"];
            
            if (id != null)
            {
                CentralOffice.PublicAffair.BusinessActions.PostReply.DeleteById(new Guid(id));
                return "1";
            }
            else
            {
                return "0";
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void Repeater1_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            //HtmlAnchor aEdit = e.Item.Controls[1] as HtmlAnchor;
            HtmlAnchor aEdit = e.Item.FindControl("aEdit") as HtmlAnchor;
            HtmlAnchor aDelete = e.Item.FindControl("aDelete") as HtmlAnchor;
            PostReplyRow reply = ((DataRowView)e.Item.DataItem).Row as PostReplyRow;

            // 只要有管理帖子的权限，就可以删除帖子的回复信息
            if (canManage.Value)
            {
                aDelete.Visible = true;
                aDelete.Attributes["onclick"] = "deleteComment(this, '" + reply.Id.ToString() + "');";
            }

            if (reply.ReplyBy == CommonUtil.TrimAll(ApplicationContext.ActualUserName))
            {
                aEdit.Visible = true;
                aEdit.HRef = "PostReply.aspx?op=reply&id=" + reply.Id.ToString() + "&postid=" + reply.PostId.ToString();

                if (!aDelete.Visible)
                {
                    aDelete.Visible = true;
                    aDelete.Attributes["onclick"] = "deleteComment(this, '" + reply.Id.ToString() + "');";
                }
            }

            if (reply["Attachments"] != DBNull.Value && reply.Attachments.Length > 0)
            {
                Literal lblAttachments = e.Item.FindControl("lblAttachments") as Literal;
                HyperLink hlAttachments = e.Item.FindControl("hlAttachments") as HyperLink;

                lblAttachments.Text = "<br />附件：";
                SiteUtil.GenernateDownloadHyperLink(hlAttachments, reply.Attachments);
                lblAttachments.Visible = true;
                hlAttachments.Visible = true;
            }

            // href='PostReply.aspx?id=<%# Eval("Id") %>&postid=<%# Eval("PostId") %>'
            // onclick="deleteComment(this, '<%# Eval("Id") %>');"
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

        }
    }
}
